package com.cebbank.sz.cebtemplate.utils;

//import com.alibaba.fastjson.JSON;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Base64;

/**
 * @author daigl
 * @create 2019/12/24 8:55
 */
public class Base64Util {
    private static String charSet = "UTF-8";
    private static Base64.Encoder base64Encoder = Base64.getEncoder();
    private static Base64.Decoder base64Decoder = Base64.getDecoder();

    public static String encodeBase64(Object obj) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
            return base64Encoder.encodeToString(jsonString.getBytes(charSet));
        } catch (UnsupportedEncodingException | JsonProcessingException e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String decodeBase64(String str)  {
        try {
            return new String(base64Decoder.decode(str),charSet);
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }
}
